﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using Newtonsoft.Json;
using System.Text;
using System.IO;
using Model;

/// <summary>
///JsonHelper 的摘要说明
/// </summary>
public class JsonHelper
{
	public JsonHelper()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}

    public static string CommentToJSON(List<Comment> list, string dtName)
    {
        StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter(sb);

        using (JsonWriter jw = new JsonWriter(sw))
        {
            JsonSerializer ser = new JsonSerializer();
            jw.WriteStartObject();
            jw.WritePropertyName(dtName);
            jw.WriteStartArray();
            if (list != null)
            {
                foreach (Comment i in list)
                {
                    jw.WriteStartObject();

                    jw.WritePropertyName("CID");
                    ser.Serialize(jw, i.CID.ToString());

                    jw.WritePropertyName("Content");
                    ser.Serialize(jw, i.Content.ToString());

                    jw.WritePropertyName("Content");
                    ser.Serialize(jw, i.Content.ToString());

                    jw.WritePropertyName("CreatedTime");
                    ser.Serialize(jw, i.CreatedTime.ToString());

                    jw.WritePropertyName("CreatedBy");
                    ser.Serialize(jw, i.CreatedBy);

                    jw.WritePropertyName("IID");
                    ser.Serialize(jw, i.IID.ToString());

                    jw.WritePropertyName("FName");
                    ser.Serialize(jw, i.FName.ToString());

                    jw.WritePropertyName("LName");
                    ser.Serialize(jw, i.LName.ToString());

                    jw.WriteEndObject();
                }
            }
            jw.WriteEndArray();
            jw.WriteEndObject();

            sw.Close();
            jw.Close();
        }
        return sb.ToString();
    }

    public static string ObjectToJSON(List<Idea> list, string dtName)
    {
        StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter(sb);

        using (JsonWriter jw = new JsonWriter(sw))
        {
            JsonSerializer ser = new JsonSerializer();
            jw.WriteStartObject();
            jw.WritePropertyName(dtName);
            jw.WriteStartArray();
            if (list != null)
            {
                foreach (Idea i in list)
                {
                    jw.WriteStartObject();

                    jw.WritePropertyName("IID");
                    ser.Serialize(jw, i.IID.ToString());

                    jw.WritePropertyName("Title");
                    ser.Serialize(jw, i.Title.ToString());

                    jw.WritePropertyName("Content");
                    ser.Serialize(jw, i.Content.ToString());

                    jw.WritePropertyName("Attachments");
                    ser.Serialize(jw, i.Attachments.ToString());

                    jw.WritePropertyName("CategoryType");
                    ser.Serialize(jw, i.CategoryName);

                    jw.WritePropertyName("Status");
                    ser.Serialize(jw, i.StatusName);

                    jw.WritePropertyName("Votes");
                    ser.Serialize(jw, i.Votes.ToString());

                    jw.WritePropertyName("CreatedBy");
                    ser.Serialize(jw, i.CreatedBy.ToString());

                    jw.WritePropertyName("CreatedTime");
                    ser.Serialize(jw, i.CreatedTime.ToString());

                    jw.WritePropertyName("FirstName");
                    ser.Serialize(jw, i.FirstName.ToString());

                    jw.WritePropertyName("LastName");
                    ser.Serialize(jw, i.LastName.ToString());

                    jw.WritePropertyName("MapTypes");
                    ser.Serialize(jw, i.MapTypes.ToString());

                    jw.WritePropertyName("CommentNum");
                    ser.Serialize(jw, i.CommentNum.ToString());

                    jw.WritePropertyName("State");
                    ser.Serialize(jw, i.Status.ToString());

                    jw.WriteEndObject();
                }
            }
            jw.WriteEndArray();
            jw.WriteEndObject();

            sw.Close();
            jw.Close();

        }

        return sb.ToString();
    }
}
